package com.douqu.game.core.entity.db;

import com.bean.core.buffer.ByteBuffer;
import com.bean.core.util.TimeUtils;
import com.douqu.game.core.config.common.GoodsData;
import com.douqu.game.core.entity.EntityObject;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author: Gavin.
 * Description:
 * Date: 2017/12/2 0002 下午 12:26
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class MailDB extends EntityObject {

    public int id;

    /***
     * 标题
     */
    public String title;

    /***
     * 内容
     */
    public String content;
    /***
     * 创建时间
     */
    public long sendTime;

    /**
     * 自己的角色类型
     */
    public int roleType;

    /***
     * 对方标示(是玩家的时候是对方玩家的流水号,如果是系统则是-1)
     *
     */
    public String target;

    /**
     * 邮件状态是否已读
     */
    public boolean isRead;

    /***
     * 附件是否领取
     */
    public boolean isGet;

    /***
     * 附件
      */
    private List<GoodsData> attachList;


    public MailDB()
    {
        attachList = new CopyOnWriteArrayList<>();
    }

    public MailDB(String title, String content, List<GoodsData> attachList)
    {
        this.title = title;
        this.content = content;
        this.roleType = SGCommonProto.E_MAIL_SENDER_TYPE.MAIL_SENDER_TYPE_RECEIVER_VALUE;
        this.sendTime = DataFactory.currentTime;
        this.target = "-1";
        if(attachList != null){
            this.attachList = attachList;
        }else {
            this.attachList = new CopyOnWriteArrayList<>();
        }
    }

    public void reset() {
        attachList = new CopyOnWriteArrayList<>();
    }

    public void loadFrom(ByteBuffer buffer) {

        id = buffer.readInt();
        title = buffer.readUTF();
        content = buffer.readUTF();
        sendTime = buffer.readLong();
        roleType = buffer.readByte();
        target = buffer.readUTF();
        isRead = buffer.readBoolean();
        isGet = buffer.readBoolean();
        int size = buffer.readShort();
        GoodsData goodsData = null;
        for(int i = 0; i < size ; i ++){
            goodsData = new GoodsData();
            goodsData.loadFrom(buffer);
            attachList.add(goodsData);
        }
    }

    public void writeTo(ByteBuffer buffer) {
        buffer.writeInt(id);
        buffer.writeUTF(title);
        buffer.writeUTF(content);
        buffer.writeLong(sendTime);
        buffer.writeByte(roleType);
        buffer.writeUTF(target);
        buffer.writeBoolean(isRead);
        buffer.writeBoolean(isGet);
        buffer.writeShort(attachList.size());
        for(GoodsData goodsData : attachList){
            goodsData.writeTo(buffer);
        }
    }

    public void addAttach(GoodsData goodsData)
    {
        attachList.add(goodsData);
    }

    public List<GoodsData> getAttachList() {
        return attachList;
    }

    public void setAttachList(List<GoodsData> attachList) {
        if(attachList != null){
            this.attachList = attachList;
        }

    }

    @Override
    public String toString() {
        return "MailDB{" +
                "id='" + id + '\'' +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", sendTime=" + TimeUtils.getFormatTime(sendTime) +
                ", target='" + target + '\'' +
                ", isRead=" + isRead +
                ", attachList=" + attachList +
                ", isGet=" + isGet +
                '}';
    }

}
